import java.applet.Applet;
import com.turningtech.poll.Poll;
import com.turningtech.poll.PollService;
import com.turningtech.poll.Response;
import com.turningtech.poll.ResponseListener;
import com.turningtech.receiver.Receiver;
import com.turningtech.receiver.ReceiverService;
import com.turningtech.receiver.ResponseCardLibrary;
import netscape.javascript.JSObject;

public class KeypadListener extends Applet {

	public Poll poll;
	public int questionID;
	public String responseHandler;
	private static final long serialVersionUID = -9221219260470485552L;

	public void init() {
		try {
			ResponseCardLibrary.initializeLicense("University of Queensland", "24137BBFEEEA9C7F5D65B2432F10F960");
			int pollChannel = Integer.parseInt(getParameter("pollChannel"));
			JSObject window = JSObject.getWindow(this);
			this.questionID = -1;
			this.responseHandler = (String)window.getMember("responseHandler");
			Receiver receiver = ReceiverService.findReceivers().get(0);
			receiver.setChannel(pollChannel);
			this.poll = PollService.createPoll();
			this.poll.addResponseListener(new KeypadResponseListener());
			MonitorPoll p = new MonitorPoll(this);
			p.start();
		}
		catch (Exception e) {
			JSObject window = JSObject.getWindow(this);
			window.eval("keypadListenerError()");
		}
	}

	public void destroy() {
		if (this.poll != null) {
			this.poll.shutdown();
		}
	}

	private Poll.PollingMode getPollingMode(String questionType) {
		if (questionType.equals("singleResponse")) {
			return Poll.PollingMode.SingleResponse;
		}
		if (questionType.equals("multiResponse")) {
			return Poll.PollingMode.MultiResponse_Numeric;
		}
		if (questionType.equals("numericResponse")) {
			return Poll.PollingMode.Numeric;
		}
		return Poll.PollingMode.SingleResponse;
	}

	public void callJavascript(String deviceID, String response) {
		JSObject window = JSObject.getWindow(this);
		window.eval(responseHandler + "(\"" + deviceID + "\", \"" + response + "\")");
	}

	private class KeypadResponseListener implements ResponseListener {

		public KeypadResponseListener() {
		}

		public void responseReceived(Response response) {
			callJavascript(response.getResponseCardId(), response.getResponse());
		}
	}

	private class MonitorPoll extends Thread {

		KeypadListener keypadListener;

		public MonitorPoll(KeypadListener listener) {
			this.keypadListener = listener;
		}

		public void run() {
			monitor();
		}

		public synchronized void monitor() {
			while (true) {
				JSObject window = JSObject.getWindow(keypadListener);
				try {
					wait(500);
				}
				catch (Exception e) {
				}
				int currentQuestionID = -1;
				try {
					currentQuestionID = (Integer)window.getMember("questionID");
				}
				catch (Exception e) {
					currentQuestionID = Integer.parseInt((String)window.getMember("questionID"));
				}
				if (currentQuestionID != keypadListener.questionID) {
					keypadListener.questionID = currentQuestionID;
					String questionType = (String)window.getMember("questionType");
					if (!questionType.equals("none") && currentQuestionID != -1) {
						keypadListener.responseHandler = (String)window.getMember("responseHandler");
						keypadListener.poll.start(keypadListener.getPollingMode(questionType));
					}
					else {
						keypadListener.poll.stop();
					}
				}
			}
		}
	}
}
